import 'package:efood_multivendor/controller/splash_controller.dart';
import 'package:efood_multivendor/util/app_constants.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';

class PriceConverter {
  static String convertPrice(double price,
      {double? discount,
      String? discountType,
      int asFixed = AppConstants.DIGITS_AFTER_POINT}) {
    if (discount != null && discountType != null) {
      if (discountType == 'amount') {
        price = price - discount;
      } else if (discountType == 'percent') {
        price = price - ((discount / 100) * price);
      }
    }
    String currencySymbol = "right";
    String currencySymbolValue = "br".tr;
    bool _isRightSide = currencySymbol == 'right';
    return '${_isRightSide ? '' : currencySymbolValue + ' '}'
        '${(price <= 0 ? 0 : price).toStringAsFixed(asFixed).replaceAllMapped(RegExp(r'(\d{1,3})(?=(\d{3})+(?!\d))'), (Match m) => '${m[1]},')}'
        '${_isRightSide ? ' ' + currencySymbolValue : ''}';
  }

  static double convertWithDiscount(
      double price, double discount, String discountType) {
    if (discountType == 'amount') {
      price = price - discount;
    } else if (discountType == 'percent') {
      price = price - ((discount / 100) * price);
    }
    return price;
  }

  static double calculation(
      double amount, double discount, String type, int quantity) {
    double calculatedAmount = 0;
    if (type == 'amount') {
      calculatedAmount = discount * quantity;
    } else if (type == 'percent') {
      calculatedAmount = (discount / 100) * (amount * quantity);
    }
    return calculatedAmount;
  }

  static String percentageCalculation(
      String price, String discount, String discountType) {
    return '$discount${discountType == 'percent' ? '%' : Get.find<SplashController>().configModel?.currencySymbol} OFF';
  }

  static String formatNumber(double number) {
    final formatter = NumberFormat("#,##0.0", "en_US");
    final formattedNumber = formatter.format(number);
    return '$formattedNumber';
  }
}
